package com.unknown.wificontrol.ui.images;

import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.android.material.snackbar.BaseTransientBottomBar;
import com.unknown.wificontrol.R;
import com.unknown.wificontrol.adapter.FileAdapter;
import com.unknown.wificontrol.bean.FileBean;
import com.unknown.wificontrol.ui.BaseActivity;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import butterknife.BindView;

public class HistoryActivity extends BaseActivity implements FileAdapter.OnFileViewListener {
    @Override
    public int getLayout() {
        return R.layout.layout_image_history_list;
    }

    @BindView(R.id.rvImages)
    RecyclerView rvImages;

    private Handler handler = new Handler();

    private List<FileBean> fileBeans;

    private FileAdapter fileAdapter;

    @Override
    public void initViews() {
        rvImages.setLayoutManager(new LinearLayoutManager(this));
        fileBeans = new ArrayList<>();
        fileAdapter = new FileAdapter(fileBeans);
        rvImages.setAdapter(fileAdapter);
        fileAdapter.setOnFileViewListener(this);
        loadImages();

    }

    private void loadImages() {
        showProgress();
        new Thread(new Runnable() {
            @Override
            public void run() {
                fileBeans.clear();
                File fileDir = new File(mActivity.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "wifi");
                if (fileDir != null && fileDir.exists()) {
                    File[] folders = fileDir.listFiles();
                    if (folders != null && fileDir.length() > 0) {
                        for (File folder : folders) {
                            FileBean parentFolder = new FileBean(folder.getName(), folder.getPath(), folder.lastModified());
                            File[] subFiles = folder.listFiles();
                            List<FileBean> filesBean = new ArrayList<>();
                            for (File file : subFiles) {
                                filesBean.add(new FileBean(file.getName(), file.getPath(), file.lastModified()));
                            }
                            parentFolder.files.addAll(filesBean);
                            fileBeans.add(parentFolder);
                        }
                    }
                }
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        hideProgress();
                        if (fileBeans.size() == 0) {
                            fileAdapter.setEmptyView(emptyData("~暂无图片~"));
                        }
                        Collections.sort(fileBeans, comparator);
                        fileAdapter.notifyDataSetChanged();
                    }
                });
            }
        }).start();
    }

    private Comparator comparator = new Comparator<FileBean>() {
        @Override
        public int compare(FileBean file, FileBean newFile) {
            if (file.modify < newFile.modify) {
                return 1;
            } else if (file.modify == newFile.modify) {
                return 0;
            } else {
                return -1;
            }
        }

        @Override
        public boolean equals(Object obj) {
            return false;
        }
    };

    @Override
    public void onFileView(FileBean bean) {
        Bundle bundle = new Bundle();
        bundle.putSerializable("bean", bean);
        changeView(PhotoViewActivity.class, bundle);
    }
}
